<template>
    <view style="padding: 15px;">
        <u-form ref="validateFormRef" :model="validateForm.value" :label-width="150">
            <u-form-item label="编码" prop="code" required>
                <u-input v-model="validateForm.value.code" placeholder="请输入编码"/>
            </u-form-item>
            <u-form-item label="名称" prop="name" required>
                <u-input v-model="validateForm.value.name" placeholder="请输入名称"/>
            </u-form-item>
            <u-form-item label="状态" prop="status" required>
                <u-input type="select" :select-open="validateForm.controls.status.isShow" v-model="validateForm.controls.status.textDesc"
                         placeholder="请选择状态" @click="validateForm.controls.status.isShow = true; $forceUpdate();"></u-input>
            </u-form-item>
            <u-form-item label="显示顺序" prop="displayOrder">
                <u-input v-model="validateForm.value.displayOrder" placeholder="请输入显示顺序"/>
            </u-form-item>
            <u-form-item label="备注" prop="remark">
                <u-input type="textarea" height="40" v-model="validateForm.value.remark" placeholder="请输入备注"/>
            </u-form-item>
        </u-form>

        <u-button type="error" @click="submit">提交</u-button>

        <u-select mode="single-column" label-name="name" value-name="code" :mask-close-able="false"
                  v-model="validateForm.controls.status.isShow" :list="validateForm.controls.status.options"
                  :default-value="validateForm.controls.status.defaultValue"
                  @cancel="validateForm.controls.status.isShow = false; $forceUpdate();"
                  @confirm="UFormUtil.selectSingleColumnConfirm($event, validateForm.controls.status)"></u-select>
    </view>
</template>

<script>
    import {CommonUtil} from "@/dev/core/utils/common.util";
    import {SysDictionaryService} from "@/dev/services/sys-dictionary.service";
    import {UFormUtil} from "@/dev/core/utils/u-form.util";
    import {CommonService} from "@/dev/services/common.service";
    import {TbGoodsClassService} from "@/dev/services/tb-goods-class.service";

    export default {
        data() {
            return {
                validateForm: {
                    value: {
                        storeId: this.globalVariable.userInfo.tbStore.id,
                        code: '',
                        name: '',
                        displayOrder: '',
                        status: "1",
                        remark: '',
                    },
                    rules: {
                        code: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {
                                trigger: ['blur'],
                                asyncValidator: (rule, value, callback) => {
                                    new CommonService().uniqueCheckByFields({
                                        "tableName": "tb_goods_class",
                                        "fieldList": [
                                            {"fieldName": "store_id", "fieldValue": this.validateForm.value.storeId},
                                            {"fieldName": "code", "fieldValue": value},
                                        ],
                                        "excludeId": null
                                    }).then(result => {
                                        if (result.status === "fail") {
                                            callback(new Error('输入的内容已存在！'));
                                        } else {
                                            callback();
                                        }
                                    })
                                }
                            }
                        ],
                        name: [
                            {required: true, message: '必填项!', trigger: ['change', 'blur']},
                            {pattern: /^(?=.*\S).+$/, message: '必填项!', trigger: ['change', 'blur']}
                        ],
                        displayOrder: [
                            {
                                pattern: /^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
                                message: '请输入大于0的正数!',
                                trigger: ['change', 'blur']
                            }
                        ],
                    }
                }
            }
        },
        onReady() {
            const _self = this;
            new SysDictionaryService().optionList('status', null).then(result => {
                UFormUtil.setOptions(_self.validateForm.controls.status, result);
                _self.$forceUpdate();
            });
        },
        methods: {
            submit() {
                const _self = this;
                this.$refs.validateFormRef.validate(valid => {
                    if (valid) {
                        console.log('验证通过', _self.validateForm.value);
                        CommonUtil.alert({
                            title: '提示！',
                            content: '确认提交吗？',
                            showCancel: true, // 是否显示取消按钮
                            success: function (res) {
                                if (res.cancel) { // 点击取消,默认隐藏弹框
                                } else { // 点击确定
                                    new TbGoodsClassService().saveTbGoodsClass(_self.validateForm.value).then(result => {
                                        CommonUtil.toastNavigateBack('保存成功')
                                    });
                                }
                            }
                        });
                    } else {
                        console.log('验证失败');
                    }
                });
            }
        }
    };
</script>

<style lang="scss" scoped>
</style>
